import React from 'react';
import { Button, Tooltip } from 'antd';
import { PermissionUtils } from '@/utils/permission';

interface AuthorizedButtonProps {
    code: string; // 权限码
    children: React.ReactNode;
    tooltip?: string; // 无权限时的提示
    fallback?: React.ReactNode; // 无权限时的替代组件
    [key: string]: any; // 其他 Button 属性
}

const AuthorizedButton: React.FC<AuthorizedButtonProps> = ({
                                                               code,
                                                               children,
                                                               tooltip,
                                                               fallback = null,
                                                               ...restProps
                                                           }) => {
    const hasPermission = PermissionUtils.hasButtonPermission(code);

    if (!hasPermission) {
        if (tooltip) {
            return (
                <Tooltip title={tooltip}>
          <span style={{ opacity: 0.5, cursor: 'not-allowed' }}>
            {fallback}
          </span>
                </Tooltip>
            );
        }
        return <>{fallback}</>;
    }

    return <Button {...restProps}>{children}</Button>;
};

export default AuthorizedButton;